package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.advice.OneAnnotation;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utils.BusinessException;
import com.itheima.service.ClazzService;
import com.itheima.utils.OneException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

//@Scope("prototype")
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper,Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private EmpService empService;
    @Autowired
    private StudentMapper studentMapper;

//    @Override
//    public PageResult page(ClazzQueryPaeam clazzQueryPaeam) {
//        PageHelper.startPage(clazzQueryPaeam.getPage(),clazzQueryPaeam.getPageSize());
//        //Page<Clazz> page = clazzMapper.page(clazzQueryPaeam);
////        List<Clazz> clazzList = clazzMapper.page(clazzQueryPaeam);
//
//        // 将List强转为Page（PageHelper确保这是安全的）
//        Page<Clazz> page = (Page<Clazz>) clazzMapper.page(clazzQueryPaeam);
//        long total = page.getTotal();
//        List<Clazz> rows = page.getResult();
//        return new PageResult<>(total,rows);
//    }

    @Override
    public PageResult page(ClazzQueryPaeam clazzQueryPaeam) {
        IPage<Clazz> page = new Page<>(clazzQueryPaeam.getPage(),clazzQueryPaeam.getPageSize());

        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(clazzQueryPaeam.getName() != null , Clazz::getName , clazzQueryPaeam.getName());
        queryWrapper.between(clazzQueryPaeam.getBegin() != null && clazzQueryPaeam.getEnd() != null , Clazz::getEndDate , clazzQueryPaeam.getBegin(),clazzQueryPaeam.getEnd());
        page = page(page , queryWrapper);

        List<Clazz> clazzList = page.getRecords();
        clazzList.forEach(clazz -> {
            if (clazz.getMasterId() !=null) {
                Emp emp = empService.getById(clazz.getMasterId());
                clazz.setMasterName(emp.getName());
            }
            if (clazz.getBeginDate() != null && clazz.getEndDate() != null) {
                if (clazz.getEndDate().isBefore(LocalDate.now()) ) {
                    clazz.setStatus("已结课");
                } else if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                    clazz.setStatus("未开班");
                } else {
                    clazz.setStatus("已开课");
                }
            }
        });
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteById(Integer id) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getClazzId,id);
        List<Student> studentList = studentMapper.selectList(queryWrapper);
        if(studentList.size()>0){
            throw new OneException("班级下面有学生不能删除");
        }
        this.removeById(id);
    }

    @Override
    public Clazz seleteId(Integer id) {
        Clazz clazz = this.getById(id);
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Emp::getId,id);
        Emp emp = empService.getOne(queryWrapper);
        if(emp!=null){
            clazz.setMasterName(emp.getName());
        }
        return clazz;
    }

//    @Override
//    public void delete(Integer id) {
//        int count = clazzMapper.count(id);
//
//        if (count > 0) {
//            throw new BusinessException("对不起，当前班级下有员工，不能直接删除！");
//        }
//        clazzMapper.delete(id);
//    }
//
//    @Override
//    public void add(Clazz clazz) {
//        clazz.setCreateTime(LocalDateTime.now());
//        clazz.setUpdateTime(LocalDateTime.now());
//        clazzMapper.add(clazz);
//    }
//
//    @Override
//    public Clazz seleteId(Integer id) {
//       return clazzMapper.seleteId(id);
//    }
//
//    @Override
//    public void update(Clazz clazz) {
//        clazz.setUpdateTime(LocalDateTime.now());
//        clazzMapper.update(clazz);
//    }
//
//    @Override
//    public List<Clazz> findAll() {
//        return clazzMapper.findAll();
//    }
}
